-- Fetches the player meta table for easier access --
local PMeta = FindMetaTable( "Player" )

-- User Data --

-- Checks if the user exists --
function PMeta:UserExists()
	if ( Hyperion.Schema.Configuration[ "MySQL_Mode" ] == true and Hyperion.Schema.Configuration[ "SQLite_Mode" ] == false ) then
		local table = { }
		local query = HyperionDB:query( "SELECT `SteamID` FROM `hyperion_users` WHERE `SteamID` = '" .. HyperionDB:escape( self:SteamID() ) .. "'" )

		query.onSuccess = function()
			table = query:getData()

			if ( table ) then
				return true
			end
		end

		query.onError = function( q, e )
			MsgN( "[Hyperion] SQL Error: " .. e )
		end

		query:start()
		query:wait()
	elseif ( Hyperion.Schema.Configuration[ "SQLite_Mode" ] == true and Hyperion.Schema.Configuration[ "MySQL_Mode" ] == false ) then
		local query = sql.Query( "SELECT `SteamID` FROM `hyperion_users` WHERE `SteamID` = '" .. SQLStr( self:SteamID() ) .. "'" )

		if ( query ) then
			return true
		end
	end
end

-- Automatically creates a new user --
function PMeta:CreateUser( steamid, name )
	if ( Hyperion.Schema.Configuration[ "MySQL_Mode" ] == true and Hyperion.Schema.Configuration[ "SQLite_Mode" ] == false ) then
		if ( !self:UserExists() ) then
			local query = HyperionDB:query( "INSERT INTO `hyperion_users` (`SteamID`, `Name`) VALUES ('" .. HyperionDB:escape( steamid ) .. "', '" .. HyperionDB:escape( name ) .. "'" )

			query.onError = function( q, e )
				MsgN( "[Hyperion] SQL Error: " .. e )
			end

			query:start()
			query:wait()
		end
	elseif ( Hyperion.Schema.Configuration[ "SQLite_Mode" ] == true and Hyperion.Schema.Configuration[ "MySQL_Mode" ] == false ) then
		if ( !self:UserExists() ) then
			sql.Query( "INSERT INTO `hyperion_users` (`SteamID`, `Name`) VALUES ('" .. SQLStr( steamid ) .. "', '" .. SQLStr( name ) .. "'" )
		end
	end
end

-- Character Data --

